<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use DB;
use App\Components\DyVideoTools;
use App\Models\DyModels\HandleVideo;
use App\Models\UserModels\DyAccessToken;
use App\Models\UserModels\User;
use App\Components\FileTools;
use App\Models\DyModels\UserVideo;
/**
 * 大于50M视频上传
 */
class DyBigVideoUpload extends Command{
  /**
   * The name and signature of the console command.
   *
   * @var string
   */
  protected $signature = 'DyBigVideoUpload';

  /**
   * The console command description.
   *
   * @var string
   */
  protected $description = '大于50M视频上传';
  
  /**
   * Execute the console command.
   *
   * @return mixed
   */
  public function handle(){
    $this->demo();
  }
  
  public function demo(){
    $partner_id = 1;
    $oDyAccessToken = new \App\Models\UserModels\DyAccessToken();
    $datData = $oDyAccessToken->db_get(['partner_id'=>['=',$partner_id]]);
    $oUser = new \App\Models\UserModels\User();
    $uData = $oUser->db_get(['partner_id'=>['=',$partner_id]]);
    $video_parts = [
      'http://qhls.audio.xiaozhangbang.org/AyF-eWH5m_vEcRUMaVJny267fmk=/lpO9vtQEV0Ewl0Y_E1DYQj92G4_9/000000.ts',
      'http://qhls.audio.xiaozhangbang.org/AyF-eWH5m_vEcRUMaVJny267fmk=/lpO9vtQEV0Ewl0Y_E1DYQj92G4_9/000001.ts',
      'http://qhls.audio.xiaozhangbang.org/AyF-eWH5m_vEcRUMaVJny267fmk=/lpO9vtQEV0Ewl0Y_E1DYQj92G4_9/000002.ts',
      'http://qhls.audio.xiaozhangbang.org/AyF-eWH5m_vEcRUMaVJny267fmk=/lpO9vtQEV0Ewl0Y_E1DYQj92G4_9/000003.ts',
      'http://qhls.audio.xiaozhangbang.org/AyF-eWH5m_vEcRUMaVJny267fmk=/lpO9vtQEV0Ewl0Y_E1DYQj92G4_9/000004.ts',
      'http://qhls.audio.xiaozhangbang.org/AyF-eWH5m_vEcRUMaVJny267fmk=/lpO9vtQEV0Ewl0Y_E1DYQj92G4_9/000005.ts',
    ];
    if(!empty($datData) && !empty($uData)){
      foreach ($video_parts as $key => $val){
        DyVideoTools::videoPartUpload($datData['access_token'],$uData['open_id'],$val,$key+1);
      }
      $res = DyVideoTools::videoPartComplete($datData['access_token'],$uData['open_id']);
      if($res['data']['error_code'] == 0){
        DyVideoTools::videoCreate($datData['access_token'],$uData['open_id'],$res['data']['video']['video_id'],'2021离开我也就是');
      }
    }
    exit;
  }
  
  public function saveBigVideo(){
    $key = 'handle_big_video_cache';
    $cache = Cache::get($key);
    if(empty($cache)){
      //获取小类型视频
      $videos = DB::tbale('pt_handle_video')->where('type',1)->where('size_type',2)->where('deal_status',4)->where('status',0)->get();
      $expiresAt = Carbon::now()->addHour();
      if(!empty($videos)){
        $oHandleVideo = new HandleVideo();
        $oDyAccessToken = new DyAccessToken();
        $oUser = new User();
        $oFileTools = new FileTools();
        foreach($videos as $val){
          $oHandleVideo->db_update('',$val['id'],['status'=>1,'update_time'=>date('Y-m-d H:i:s')]);
          $datData = $oDyAccessToken->db_get(['partner_id'=>['=',$val['partner_id']]]);
          $uData = $oUser->db_get(['partner_id'=>['=',$val['partner_id']]]);
          if(!empty($datData) && !empty($uData)){
            $custom_cover_image_url = '';
            if(!empty($val['cover'])){
              $carr = explode('/',$val['cover']);
              $cname = end($carr);
              $cover_path = $oFileTools->cdn_root.$val['cover'];
              $res0 = DyVideoTools::imageUpload($datData['access_token'],$uData['open_id'],$cover_path,$cname);
              if($res0['data']['error_code'] == 0){
                $custom_cover_image_url = $res0['data']['image']['image_id'];
              }
            }
            $video_path = $oFileTools->cdn_root.$val['video_url'];
            $varr = explode('/',$val['video_url']);
            $vname = end($varr);
            $res = DyVideoTools::videoUpload($datData['access_token'],$uData['open_id'],$video_path,$vname,$custom_cover_image_url);
            if($res['data']['error_code'] == 0){  //视频上传成功
              $video_id = $res['data']['video']['video_id'];
              $video_title = $val['title'].$val['talk'];
              $res2 = DyVideoTools::videoCreate($datData['access_token'],$uData['open_id'],$video_id,$video_title);
              if($res2['extra']['error_code'] == 0){  //视频发布成功
                $data = [];
                $data['partner_id'] = $val['partner_id'];
                $data['title'] = $val['title'].$val['talk'];
                $data['item_id'] = $res2['data']['item_id'];
                $data['is_reviewed'] = 0;
                $data['video_status'] = 4;
                $data['create_time'] = $val['create_time'];
                $data['update_time'] = date('Y-m-d H:i:s');
                $oUserVideo = new UserVideo();
                if($oUserVideo->db_insert($data)){
                  $oHandleVideo->db_update('',$val['id'],['status'=>4,'update_time'=>date('Y-m-d H:i:s')]);
                }else{
                  $oHandleVideo->db_update('',$val['id'],['status'=>3,'update_time'=>date('Y-m-d H:i:s')]);
                }
              }else{
                $oHandleVideo->db_update('',$val['id'],['status'=>3,'update_time'=>date('Y-m-d H:i:s')]);
              }
            }else{
              $oHandleVideo->db_update('',$val['id'],['status'=>3,'update_time'=>date('Y-m-d H:i:s')]);
            }
            Cache::put($key,0,$expiresAt);
          }else{//没有找到会员授权信息
            $oHandleVideo->db_update('',$val['id'],['status'=>2,'update_time'=>date('Y-m-d H:i:s')]);
          }
        }
        Cache::put($key,1,$expiresAt);
      }else{
        Cache::put($key,0,$expiresAt);
      }
    }
  }
}
